﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Model;
using Maticsoft.DBUtility;//请先添加引用
using System.Data.SqlClient;

namespace DAL
{
    public class CardUserDAO
    {
        public CardUserDAO() { }
        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(CardUser model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into CardUser(");
            strSql.Append("FromUserName,Name,Phone,ToUserName)");
            strSql.Append(" values (");
            strSql.Append("@FromUserName,@Name,@Phone,@ToUserName)");
            SqlParameter[] parameters = {
					new SqlParameter("@FromUserName", SqlDbType.VarChar,50),
					new SqlParameter("@Name", SqlDbType.VarChar,50),
					new SqlParameter("@Phone", SqlDbType.VarChar,50),
                                        new SqlParameter("@ToUserName",SqlDbType.VarChar,50)};
            parameters[0].Value = model.FromUserName;
            parameters[1].Value = model.Name;
            parameters[2].Value = model.Phone;
            parameters[3].Value = model.ToUserName;

            return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public int Update(CardUser model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update CardUser set ");
            strSql.Append("ID=@ID,");
            strSql.Append("FromUserName=@FromUserName,");
            strSql.Append("Name=@Name,");
            strSql.Append("Phone=@Phone,ToUserName=@ToUserName");
            strSql.Append(" where ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int,4),
					new SqlParameter("@FromUserName", SqlDbType.VarChar,50),
					new SqlParameter("@Name", SqlDbType.VarChar,50),
					new SqlParameter("@Phone", SqlDbType.VarChar,50),
                                        new SqlParameter("@ToUserName",SqlDbType.VarChar,50)};
            parameters[0].Value = model.ID;
            parameters[1].Value = model.FromUserName;
            parameters[2].Value = model.Name;
            parameters[3].Value = model.Phone;
            parameters[4].Value = model.ToUserName;

            return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public void Delete(int id)
        {
            //该表无主键信息，请自定义主键/条件字段
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from CardUser ");
            strSql.Append(" where id="+id);
            SqlParameter[] parameters = {
};

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CardUser GetModel(string FromUserName)
        {
            //该表无主键信息，请自定义主键/条件字段
            StringBuilder strSql = new StringBuilder();
            strSql.Append(string.Format("select  top 1 ID,FromUserName,Name,Phone from CardUser  where FromUserName='{0}'",FromUserName));
            SqlParameter[] parameters = {};

            Model.CardUser model = new Model.CardUser();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ID"].ToString() != "")
                {
                    model.ID = int.Parse(ds.Tables[0].Rows[0]["ID"].ToString());
                }
                model.FromUserName = ds.Tables[0].Rows[0]["FromUserName"].ToString();
                model.Name = ds.Tables[0].Rows[0]["Name"].ToString();
                model.Phone = ds.Tables[0].Rows[0]["Phone"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ID,FromUserName,Name,Phone ");
            strSql.Append(" FROM CardUser ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" ID,FromUserName,Name,Phone ");
            strSql.Append(" FROM CardUser ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }

    }
}
